(function(){
    "use strict";
    var developProcessController=function ($scope,$http,$modal,develop_processService,online_testService,
        objectIdService,prosService,opersService){
        //初始化项目ID号
        $scope.projectId = '';
        //初始化项目下所有的产品信息
        $scope.pros = [];
        //初始化项目下所有的产品的所有需求处理人信息
        $scope.operators = [];
        //获取所有全局变量的值
        $scope.rootParasGet1 = function (){
            $scope.projectId = objectIdService('0');
            $scope.pros = prosService([],true);
            $scope.operators = opersService([],true);
        };

        //初始产品ID,从load1方法中获取
        $scope.initproductionId = '';

        //初始化状态参数
        $scope.status = ["所有状态","未开始","处理中","已完成"];

        //初始化需求类型参数
        $scope.kinds = ["所有类型","任务","缺陷","改进"];

        //初始化优先级类型参数
        $scope.prioritys = ["所有优先级","高","中","低"];


        //初始处理人ID,从load1方法中获取
        $scope.initOperatorId = '';
        //根据产品，处理人，状态，类型，优先级级别，关键字查找对应产品的所有需求信息
        $scope.requirementsSearch = function (request){
            develop_processService.requirementsGet(request).success(function(data){
                if(true){
                    $scope.requirements = data.result;
                }
            });
        };

        $scope.operators = [];
        //显示的需求信息
        $scope.requirements = [];

        //修改选中的产品
        $scope.proChange = function (){
            console.log("当前选择的产品ID:"+$scope.selectedPro.id+$scope.selectedPro.productName);
            $scope.initproductionId = $scope.selectedPro.id;
//            $scope.operators = $scope.opersGet();
            if($scope.operators.length>0){
                $scope.initOperatorId =$scope.operators[0].operatorId;
            }
            //根据下拉框的选项显示符合要求的需求信息
            var personkey = "";
            if (!typeof($scope.selectedOper) == undefined){
                personkey = $scope.selectedOper.nameKey
            }
            var requestDev = {
                projectId: $scope.projectId,
                productKey:$scope.selectedPro.productKey,
                personKey:personkey,
                productStatus:$scope.currentVaule($scope.selectedStatus,$scope.status[0]),
                productType:$scope.currentVaule($scope.selectedKind,$scope.kinds[0]),
                level:$scope.currentVaule($scope.selectedPriority,$scope.prioritys[0])
            }
            $scope.requirements = $scope.requirementsSearch(requestDev);
            //显示各个状态需求的数量
            $scope.requirementSummary();
        };

        $scope.currentVaule = function (para1,para2){
            if(para1 == $scope.status[0] || para1 == $scope.kinds[0] || para1 == $scope.prioritys[0]){
                return "";
            }else{
                return para1;
            }
        };

        //修改选择的处理人
        $scope.operChange = function (){
            //根据下拉框的选项显示符合要求的需求信息
            var personkey = "";
            if (!typeof($scope.selectedOper) == undefined){
                personkey = $scope.selectedOper.nameKey
            }
            var requestDev = {
                projectId:$scope.projectId,
                productKey:$scope.selectedPro.productKey,
                personKey:personkey,
                productStatus:$scope.currentVaule($scope.selectedStatus,$scope.status[0]),
                productType:$scope.currentVaule($scope.selectedKind,$scope.kinds[0]),
                level:$scope.currentVaule($scope.selectedPriority,$scope.prioritys[0])
            }
            $scope.requirementsSearch(requestDev);
            //显示各个状态需求的数量
            $scope.requirementSummary();
        };

        //修改选择的需求状态、优先级、类型以及关键字搜索
        $scope.statusChange = function (){
            //根据下拉框的选项显示符合要求的需求信息
            var personkey = "";
            if (!typeof($scope.selectedOper) == undefined){
                personkey = $scope.selectedOper.nameKey
            }
            var requestDev = {
                projectId:$scope.projectId,
                productKey:$scope.selectedPro.productKey,
                personKey:personkey,
                productStatus:$scope.currentVaule($scope.selectedStatus,$scope.status[0]),
                productType:$scope.currentVaule($scope.selectedKind,$scope.kinds[0]),
                level:$scope.currentVaule($scope.selectedPriority,$scope.prioritys[0])
            }
            $scope.requirements = $scope.requirementsSearch(requestDev);
            //显示各个状态需求的数量
            $scope.requirementSummary();
        };

        $scope.todoRequirementNum = 0;
        $scope.doingRequirementNum = 0;
        $scope.doneRequirementNum = 0;
        //统计total,doing,done这三种状态的需求数量
        $scope.requirementSummary = function (){
            //每次调用前就初始化为0
            $scope.todoRequirementNum = 0;
            $scope.doingRequirementNum = 0;
            $scope.doneRequirementNum = 0;
            angular.forEach($scope.requirements,function (item) {
                if(item.status == '未开始'){
                    $scope.todoRequirementNum++;
                }else if(item.status == '处理中'){
                    $scope.doingRequirementNum++;
                }else if(item.status == '已完成'){
                    $scope.doneRequirementNum++;
                }
            });
        };


        //第二个tab页面（代码审计）部分
        $scope.timedimensions = ["DAY","WEEK","MONTH"];
        $scope.init2ndproductionId = '';

        //获取代码审计结果
        $scope.codeInfoGet = function(projectId,productId,timeType){
            var result = [];
            var paras = {
                projectId : projectId,
                productId : productId,
                timeType : timeType
            };
            develop_processService.codeInfoGet(paras).success(function(data){
                if(data.result.length > 0 ){
                    var result = data.result;
                    $scope.clearTable();
                    $scope.initTable(result);
                }
            });
        };

        $scope.chart = new h5Charts.SerialChart();
        $scope.chart1 = new h5Charts.SerialChart();
        $scope.chart2 = new h5Charts.SerialChart();
        //画柱形图表
        $scope.initTable = function(codeInfoList){
            if(codeInfoList.length==0){
                return;
            }

            var dataProvider = [];
            var dataProvider1 = [];
            var dataProvider2 = [];
            var colorList = [];
            angular.forEach(codeInfoList,function (item) {
                var obj = {};
                obj.name = item.coderName ;
                obj.num = item.requirementsNum ;
                dataProvider.push(obj);
                colorList.push("#00B47A");

                var obj1 = {};
                obj1.name = item.coderName ;
                obj1.num = item.codeNumIncreased ;
                dataProvider1.push(obj1);

                var obj2 = {};
                obj2.name = item.coderName ;
                obj2.num = item.costTime ;
                dataProvider2.push(obj2);
            });

            $scope.chart.dataProvider = dataProvider;
            $scope.chart.categoryField = "name";
            $scope.chart.valueField = "num";
            $scope.chart.type = "column";
            $scope.chart.columnWidth = 10;
            $scope.chart.colors = colorList;
            $scope.chart.addTo("requirementdone");

            $scope.chart1.dataProvider = dataProvider1;
            $scope.chart1.categoryField = "name";
            $scope.chart1.valueField = "num";
            $scope.chart1.type = "column";
            $scope.chart1.columnWidth = 10;
            $scope.chart1.colors = colorList;
            $scope.chart1.addTo("codenumsubmit");

            $scope.chart2.dataProvider = dataProvider2;
            $scope.chart2.categoryField = "name";
            $scope.chart2.valueField = "num";
            $scope.chart2.type = "column";
            $scope.chart2.columnWidth = 10;
            $scope.chart2.colors = colorList;
            $scope.chart2.addTo("costtime");
        };

        //清空画布内容
        $scope.clearTable = function (){
            var canvas1=document.getElementById("requirementdone");
            var cxt1=canvas1.getContext("2d");
            cxt1.clearRect(0,0,canvas1.width,canvas1.height);

            var canvas2=document.getElementById("codenumsubmit");
            var cxt2=canvas2.getContext("2d");
            cxt2.clearRect(0,0,canvas2.width,canvas2.height);

            var canvas3=document.getElementById("costtime");
            var cxt3=canvas3.getContext("2d");
            cxt3.clearRect(0,0,canvas3.width,canvas3.height);
        };

        //局部刷新第二个tab页面的方法
        // $scope.rootParasGet2 = function (){
        //     $scope.load2();
        // };
        //第二个tab页面初始化方法
        // $scope.load2 = function (){
        //     if($scope.pros.length>0){
        //         $scope.init2ndproductionId =$scope.pros[0].productKey;
        //     }
        //     $scope.codeInfoGet($scope.projectId,$scope.init2ndproductionId,$scope.timedimensions[0]);
        // };

        //改变选中的产品
        $scope.pro1Change =  function (){
            var codeInfoList = $scope.codeInfoGet(
                $scope.projectId,
                $scope.selectedPro1.productKey,
                $scope.currentVaule($scope.selectedTimedimension,$scope.timedimensions[0])
                );
        };

        //改变选中的时间度量
        $scope.timedimensionChange = function (){
            var codeInfoList = $scope.codeInfoGet(
                $scope.projectId,
                $scope.currentVaule($scope.selectedPro1.productKey,$scope.init2ndproductionId),
                $scope.selectedTimedimension
                );
        };

        $scope.latestBuildRecords = [];
        //第三个tab页面（构建信息）部分

        //将boolean变量值转化成白字
        $scope.transferResult = function (var1){
            if(var1){
                return "成功" ;
            }else {
                return "失败" ;
            }
        };
        //局部刷新第三个tab页面的方法
        $scope.rootParasGet3 = function (){
            $scope.load3();
        };

        //页面加载初始方法
        $scope.load3 =  function (){
            var prosId = [];
            angular.forEach($scope.pros,function (item) {
                prosId.push(item.id);
            });
            var paras = {
                projectId : $scope.projectId,
                productIdList : prosId
            };
            var results = [];
            develop_processService.latestBuildInfo(paras).success(function(data){
                if(true){
                    results = data.result;
                }
            });

            angular.forEach(results,function (item) {
                var tem = {
                    productName : item.productName,
                    date : item.date,
                    isCheckOut : $scope.transferResult(item.isCheckOut),
                    isBuild : $scope.transferResult(item.isBuild),
                    highPriorityNum : item.highPriorityNum,
                    normalPriorityNum : item.normalPriorityNum,
                    coverageReport : item.coverageReport
                }
                $scope.latestBuildRecords.push(tem);
            });
        };

    };

    angular.module("cicd.developprocess",[])
        .controller("DevelopProcessController",["$scope","$http","$modal","develop_processService","online_testService",
            "objectIdService","prosService","opersService",developProcessController])

})();